//
//  SetPinPasswordViewController.m
//  SAILIN
//
//  Created by 崔国冠 on 2018/10/24.
//  Copyright © 2018年 赛麟. All rights reserved.
//

#import "SetPinPasswordViewController.h"
#import "ConfirmPayPasswordViewController.h"
#import "PasswordTextField.h"

//密码位数
static NSInteger const kDotsNumber = 6;

//假密码点点的宽和高  应该是等高等宽的正方形 方便设置为圆
static CGFloat const kDotWith_height = 10;

@interface SetPinPasswordViewController ()
//密码输入文本框
@property (nonatomic,strong) PasswordTextField *passwordField;
//用来装密码圆点的数组
@property (nonatomic,strong) NSMutableArray *passwordDotsArray;

@property (strong, nonatomic) UILabel* hintLabel;

@end

@implementation SetPinPasswordViewController

#pragma mark - Life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];

}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [[IQKeyboardManager sharedManager] setEnable:NO];
    
    [self initUI];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    
    [[IQKeyboardManager sharedManager] setEnable:YES];
    
}
#pragma mark - UI
- (void)initUI{
    
    [self hintLabel];
    
    [self addDotsViews];
    
    [self.view addSubview:self.passwordField];
    
    [self.passwordField becomeFirstResponder];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setTitle:@"设置PIN密码"];
    
}

- (void)passwordFieldDidChange:(UITextField *)field
{
    [self setDotsViewHidden];
    
    for (int i = 0; i < _passwordField.text.length; i ++)
    {
        if (_passwordDotsArray.count > i )
        {
            UIView *dotView = _passwordDotsArray[i];
            [dotView setHidden:NO];
        }
    }
    
    
    if (_passwordField.text.length == 6)
    {
        ConfirmPayPasswordViewController* VC = [[ConfirmPayPasswordViewController alloc]init];
        VC.password = _passwordField.text;
        [self.navigationController pushViewController:VC animated:NO];
        
    }
}
#pragma mark - private method
- (void)addDotsViews
{
    //密码输入框的宽度
    CGFloat passwordFieldWidth = CGRectGetWidth(self.passwordField.frame);
    //六等分 每等分的宽度
    CGFloat password_width = passwordFieldWidth / kDotsNumber;
    //密码输入框的高度
    CGFloat password_height = CGRectGetHeight(self.passwordField.frame);
    
    for (int i = 0; i < kDotsNumber; i ++)
    {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(i * password_width, 0, 1, password_height)];
        line.backgroundColor = UIColorFromHex(0xdddddd, 1);
        
        [self.passwordField addSubview:line];
        
        //假密码点的x坐标
        CGFloat dotViewX = (i + 1)*password_width - password_width / 2.0 - kDotWith_height / 2.0;
        CGFloat dotViewY = (password_height - kDotWith_height) / 2.0;
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(dotViewX, dotViewY, kDotWith_height, kDotWith_height)];
        dotView.backgroundColor = UIColorFromHex(0x222222,1);
        [dotView setCornerRadius:kDotWith_height/2.0];
        dotView.hidden = YES;
        [self.passwordField addSubview:dotView];
        [self.passwordDotsArray addObject:dotView];
    }
}

- (void)cleanPassword
{
    _passwordField.text = @"";
    
    [self setDotsViewHidden];
}

//将所有的假密码点设置为隐藏状态
- (void)setDotsViewHidden
{
    for (UIView *view in _passwordDotsArray)
    {
        [view setHidden:YES];
    }
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //删除键
    if (string.length == 0)
    {
        return YES;
    }
    
    if (_passwordField.text.length >= kDotsNumber)
    {
        return NO;
    }
    
    return YES;
}

#pragma mark - 懒加载
- (UILabel*)hintLabel{
    
    if (!_hintLabel) {
        _hintLabel = [[UILabel alloc]init];
        _hintLabel.text = @"请设置6位PIN密码";
        _hintLabel.textColor = [UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
        _hintLabel.font = UIFontPingFangRegular(14);
        
        [self.view addSubview:_hintLabel];
        [_hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.view.mas_bottom).multipliedBy(0.2);
        }];
    }
    return _hintLabel;
}

- (PasswordTextField *)passwordField
{
    if (nil == _passwordField)
    {
        _passwordField = [[PasswordTextField alloc] initWithFrame:CGRectMake((SIZE_SCREEN_WIDTH - 44 * 6)/2.0, SIZE_SCREEN_HEIGHT*0.27, 44 * 6, 44)];
        _passwordField.delegate = (id)self;
        _passwordField.backgroundColor = [UIColor whiteColor];
        //将密码的文字颜色和光标颜色设置为透明色
        //之前是设置的白色 这里有个问题 如果密码太长的话 文字和光标的位置如果到了第一个黑色的密码点的时候 就看出来了
        _passwordField.textColor = [UIColor clearColor];
        [_passwordField.layer setBorderWidth:1];
        [_passwordField.layer setBorderColor:UIColorFromHex(0xdddddd, 1).CGColor];
        
        /*****注释:锯齿*****/
        _passwordField.layer.allowsEdgeAntialiasing = YES;
        _passwordField.keyboardType = UIKeyboardTypeNumberPad;
        _passwordField.secureTextEntry = YES;
        _passwordField.layer.masksToBounds = YES;
        [_passwordField addTarget:self action:@selector(passwordFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordField;
}

- (NSMutableArray *)passwordDotsArray
{
    if (nil == _passwordDotsArray)
    {
        _passwordDotsArray = [[NSMutableArray alloc] initWithCapacity:kDotsNumber];
    }
    return _passwordDotsArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
